package com.yunchao.feature.business.data.datasource.api.mapper

import com.yunchao.feature.business.data.datasource.api.model.FileTaskApiModel
import com.yunchao.feature.business.data.datasource.database.model.BusinessInfoEntityModel
import com.yunchao.feature.business.data.datasource.database.model.BusinessPackEntityModel
import com.yunchao.feature.business.domain.enum.BusinessType
import com.yunchao.feature.business.domain.model.BusinessInfoModel
import com.yunchao.feature.business.domain.model.BusinessPackModel


internal fun FileTaskApiModel.toPackInfoModel() = BusinessPackEntityModel(
   voucherTypeId = voucherTypeId.orEmpty(),
    voucherTypeName = voucherTypeName.orEmpty(),
    paperTypeID = paperTypeID.orEmpty(),
    paperTypeName = paperTypeName.orEmpty(),
    value = value.orEmpty(),
    condomId = condomId.orEmpty(),
    condomName = condomName.orEmpty(),
    editionId = editionId.orEmpty(),
    editionName = editionName.orEmpty(),
    currency = currency.orEmpty(),
    bundles = bundles.orEmpty(),
    tie = tie.orEmpty(),
    number = number.orEmpty(),
    sackMoney = sackMoney.orEmpty(),
    sackNo = sackNo.orEmpty(),
    flag = flag.orEmpty(),
    scanResult = scanResult.orEmpty(),
    mustOutFlag = "0",
    oprDT = giveDataTime.orEmpty(),
    totalMoney = sackMoney.orEmpty(),
)

internal fun toEntityModel(list: List<FileTaskApiModel>) = BusinessInfoEntityModel(
    cardID = list[0].pdaScanId.orEmpty(),
    tranTypeID = list[0].tranTypeId.orEmpty(),
    tranID = list[0].tranId.orEmpty(),
    giveOperator = list[0].giveOperator.orEmpty(),
    giveDataTime = list[0].giveDataTime.orEmpty(),
    chkUserId = list[0].chkUserId.orEmpty(),
    sequency = "",
    busiInfoList = list.map { it.toPackInfoModel() },
    packInfoList = list.map { it.toPackInfoModel() },
    packInfoPayloads = emptyList()
)


internal fun toApiModel(
    taskInfo: BusinessInfoModel,
    items: List<BusinessPackModel>
): List<FileTaskApiModel> = items.mapIndexed { index, packInfo ->
    FileTaskApiModel(
        id = "${index + 1}",
        pdaScanId = taskInfo.cardID,
        tranTypeId = getBusinessTypeId(taskInfo.tranTypeID),
        tranId = taskInfo.tranID,
        sackNo = packInfo.sackNo,
        currency = packInfo.currency,
        voucherTypeId = packInfo.voucherTypeId,
        voucherTypeName = packInfo.voucherTypeName,
        paperTypeID = packInfo.paperTypeID,
        paperTypeName = packInfo.paperTypeName,
        condomId = packInfo.condomId,
        condomName = packInfo.condomName,
        editionId = packInfo.editionId,
        editionName = packInfo.editionName,
        value = packInfo.value,
        bundles = packInfo.bundles,
        tie = packInfo.tie,
        number = packInfo.number,
        sackMoney = packInfo.sackMoney,
        flag = packInfo.flag,
        scanResult = packInfo.scanResult,
        giveOperator = taskInfo.giveOperator,
        giveDataTime = taskInfo.giveDataTime,
        chkUserId = taskInfo.chkUserId
    )
}

internal fun getBusinessType(strBusinessType: String): BusinessType {
    return when (strBusinessType) {
        "PSU" -> BusinessType.BusinessTypeMIMISIF005
        "PSS" -> BusinessType.BusinessTypeMIMISIF003
        "PSE" -> BusinessType.BusinessTypeMIMISIF001
        "PSF" -> BusinessType.BusinessTypeMIMISIF007
        else -> BusinessType.BusinessTypeMIMISIF100
    }
}

internal fun getBusinessTypeId(strBusinessType: String): String {
    return when (strBusinessType) {
        BusinessType.BusinessTypeMIMISIF005.type -> "PSU"
        BusinessType.BusinessTypeMIMISIF003.type -> "PSS"
        BusinessType.BusinessTypeMIMISIF001.type -> "PSE"
        BusinessType.BusinessTypeMIMISIF007.type -> "PSF"
        else -> BusinessType.BusinessTypeMIMISIF100.type
    }
}